package com.l2qq.common.kit;

import com.l2qq.common.AppConfig;
import org.apache.log4j.Logger;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class CommonKit {
    // 使用固定大小的线程池（避免过多并发）
    private static final ExecutorService executor = Executors.newFixedThreadPool(4);
    // 专用线程池配置
    private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(2, r -> {
        Thread t = new Thread(r, "Cache-Rebuild-Thread");
        t.setDaemon(true);
        return t;
    });
    // 防抖集合
    private static final Set<String> asyncRebuildSet = Collections.newSetFromMap(new ConcurrentHashMap<>(128));
    //public static AtomicReference<Set<String>> reloadSpecification = new AtomicReference<>(new HashSet<>());
    private static final Logger logger = Logger.getLogger(CommonKit.class);
    //public static List searchSpecificationRowCacheMap = CaffeineKit.getKeys("searchSpecificationRowCache");
    public static boolean runTimeTest;
    // @Inject
    // Dict dictDao;
    public static boolean isCloseSystem;
    public static AtomicInteger carNumber = new AtomicInteger();

    public static boolean isIsCloseSystem() {
        return isCloseSystem;
    }

    public static void setIsCloseSystem(boolean isCloseSystem) {
        CommonKit.isCloseSystem = isCloseSystem;
    }

    public static boolean getRunTimeTest() {
        boolean b = AppConfig.p.getBoolean("runTimeTest");
        System.out.println(AppConfig.p.getBoolean("runTimeTest", false));
        return AppConfig.p.getBoolean("runTimeTest", false);
    }

    public static void setRunTimeTest(boolean runTimeTest) {
        CommonKit.runTimeTest = runTimeTest;
    }

    /**
     * 派号号码
     */
    public static AtomicInteger getCarNumber() {
        return carNumber;
    }

    public static void setCarNumber(AtomicInteger carNumber) {
        CommonKit.carNumber = carNumber;
    }

    // 超时控制工具方法
    private static void withTimeout(Runnable task, long timeoutMs, String timeoutMsg) {
        Future<?> future = executor.submit(task);
        try {
            future.get(timeoutMs, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            future.cancel(true);
            e.printStackTrace();
        } catch (Exception e) {
            //log.error("任务执行异常", e);
            e.printStackTrace();
        }
    }

    public static void buildCacheList() {
        System.out.println("buildCacheList 初始化缓存");
    }
}
